
//获取应用实例
const app = getApp()

Page({
  data: {
    startDateStr: null,
    endDateStr: null,
    userId: null,
    id: null,
    realMoney: 0.0,
    reason: null,
    action: null,
    wh:0
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    if (app.globalData.userId) {
      this.setData({
        userId: app.globalData.userId
      })
    }
    var action = options.action;
    if (action != 'add') {
      var id = options.id;
      var startDateStr = options.startDateStr;
      var endDateStr = options.endDateStr;
      var realMoney = options.realMoney;
      var reason = options.reason;
      this.setData({
        id: id,
        startDateStr: startDateStr,
        endDateStr: endDateStr,
        realMoney: realMoney,
        reason: reason,
        action: action
      })
    }
    else {
      var id = options.id;
      var date = new Date();
      var year = date.getFullYear();//年
      var month = date.getMonth() + 1;//月
      month = month < 10 ? "0" + month : month;
      var startDateStr = year + '年' + month + '月';
      var endDateStr = year + '年' + month + '月';
      this.setData({
        id: id,
        startDateStr: startDateStr,
        endDateStr: endDateStr,
        realMoney: realMoney,
        reason: reason,
        action: action
      })
    }
  },
  bindStartDateChange(e) {
    this.setData({
      startDateStr: e.detail.value.replace('-','年') + '月'
    })
  },
  bindEndDateChange(e) {
    this.setData({
      endDateStr: e.detail.value.replace('-', '年') + '月'
    })
  },
  realMoneyChange(e) {
    var realMoney = e.detail.value;
    this.setData({
      realMoney: realMoney
    })
  },
  reasonChange(e) {
    var reason = e.detail.value;
    this.setData({
      reason: reason
    })
  },
  submit(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 5000
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/reduction/save',
      data: {
        startDate: that.data.startDateStr,
        endDate: that.data.endDateStr,
        userId: that.data.userId,
        id: that.data.id,
        realMoney: that.data.realMoney,
        reason: that.data.reason,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideToast();
        if (res.data.syscode == 0) {
          wx.showToast({
            title: '提交成功',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1
          });
        }
        else {
          wx.showToast({
            title: '操作失败',
            duration: 3000
          })
        }
      },
      fail(res) {
        wx.hideToast();
        wx.showToast({
          title: '操作失败，请检查网络',
          duration: 3000
        })
      }
    })
  }
});